package com.xuelang.pipeline.artifact.entry.harborv2;

import com.xuelang.pipeline.artifact.app.harborv2.ArtifactService;
import com.xuelang.pipeline.artifact.app.harborv2.request.ListArtifactQuery;
import com.xuelang.pipeline.artifact.app.harborv2.request.ListTagQuery;
import com.xuelang.pipeline.artifact.app.harborv2.request.SpecificArtifactQuery;
import com.xuelang.pipeline.artifact.app.harborv2.response.ListArtifactResponse;
import com.xuelang.pipeline.artifact.app.harborv2.response.ListTagResponse;
import com.xuelang.pipeline.artifact.app.harborv2.response.SpecificArtifactResponse;
import com.xuelang.pipeline.artifact.app.nexus3v1.xo.ApiResponseXO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * HarborV2 镜像仓库下制品包原生接口web服务
 *
 * @author xiwen.zxg
 * @since 2024/7/9
 */
@RestController
@RequestMapping("/artifact")
public class ArtifactController {

    @Autowired
    private ArtifactService artifactService;

    /**
     * 查询镜像仓库中制品包列表
     *
     * @param projectName
     * @param repositoryName
     * @param query
     * @return
     */
    @GetMapping(value = "/projects/{project_name}/repositories/{repository_name}/artifacts", produces = "application/json;charset=utf-8")
    ApiResponseXO<List<ListArtifactResponse>> listRepositoryArtifact(
            @PathVariable("project_name") String projectName,
            @PathVariable("repository_name") String repositoryName,
            @ModelAttribute ListArtifactQuery query) {
        return artifactService.listRepositoryArtifact(projectName, repositoryName, query);
    }

    /**
     * 获取镜像仓库中单个制品包
     *
     * @param projectName
     * @param repositoryName
     * @param reference
     * @param query
     * @return
     */
    @GetMapping(value = "/projects/{project_name}/repositories/{repository_name}/artifacts/{reference}", produces = "application/json;charset=utf-8")
    ApiResponseXO<SpecificArtifactResponse> getRepositorySpecificArtifact(
            @PathVariable("project_name") String projectName,
            @PathVariable("repository_name") String repositoryName,
            @PathVariable("reference") String reference,
            @ModelAttribute SpecificArtifactQuery query) {
        return artifactService.getRepositorySpecificArtifact(projectName, repositoryName, reference, query);
    }

    /**
     * 删除镜像仓库中单个制品包
     *
     * @param projectName
     * @param repositoryName
     * @param reference
     * @return
     */
    @DeleteMapping(value = "/projects/{project_name}/repositories/{repository_name}/artifacts/{reference}", produces = "application/json;charset=utf-8")
    ApiResponseXO<Void> deleteRepositorySpecificArtifact(
            @PathVariable("project_name") String projectName,
            @PathVariable("repository_name") String repositoryName,
            @PathVariable("reference") String reference) {
        return artifactService.deleteRepositorySpecificArtifact(projectName, repositoryName, reference);
    }

    /**
     * 查询镜像仓库下制品包的tag列表
     *
     * @param projectName
     * @param repositoryName
     * @param reference
     * @param query
     * @return
     */
    @GetMapping(value = "/projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/tags", produces = "application/json;charset=utf-8")
    ApiResponseXO<List<ListTagResponse>> listArtifactTag(
            @PathVariable("project_name") String projectName,
            @PathVariable("repository_name") String repositoryName,
            @PathVariable("reference") String reference,
            @ModelAttribute ListTagQuery query) {
        return artifactService.listArtifactTag(projectName, repositoryName, reference, query);
    }

    /**
     * 获取镜像仓库下制品包的单个tag
     *
     * @param projectName
     * @param repositoryName
     * @param reference
     * @param query
     * @return
     */
    @GetMapping(value = "/projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/tag", produces = "application/json;charset=utf-8")
    ApiResponseXO<ListTagResponse> getArtifactTag(
            @PathVariable("project_name") String projectName,
            @PathVariable("repository_name") String repositoryName,
            @PathVariable("reference") String reference,
            @ModelAttribute ListTagQuery query) {
        return artifactService.getArtifactTag(projectName, repositoryName, reference, query);
    }

    /**
     * 删除镜像仓库下制品包的单个tag
     *
     * @param projectName
     * @param repositoryName
     * @param reference
     * @param tagName
     * @return
     */
    @DeleteMapping(value = "/projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/tags/{tag_name}", produces = "application/json;charset=utf-8")
    ApiResponseXO<Void> deleteArtifactTag(
            @PathVariable("project_name") String projectName,
            @PathVariable("repository_name") String repositoryName,
            @PathVariable("reference") String reference,
            @PathVariable("tag_name") String tagName) {
        return artifactService.deleteArtifactTag(projectName, repositoryName, reference, tagName);
    }


    /**
     * 获取镜像仓库下单个制品包的附件信息
     *
     * @param projectName
     * @param repositoryName
     * @param reference
     * @param addition 枚举值: build_history,values.yaml,readme.md,dependencies,sbom
     * @return
     */
    @DeleteMapping(value = "/projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/additions/{addition}", produces = "application/json;charset=utf-8")
    ApiResponseXO<String> artifactAdditionInfo(
            @PathVariable("project_name") String projectName,
            @PathVariable("repository_name") String repositoryName,
            @PathVariable("reference") String reference,
            @PathVariable("addition") String addition) {
        return artifactService.artifactAdditionInfo(projectName, repositoryName, reference, addition);
    }
}
